package seismosurfer.gui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import seismosurfer.gui.panel.ConfigureSliderPanel;

import com.bbn.openmap.LightMapHandlerChild;
import com.bbn.openmap.gui.OpenMapFrame;
import com.bbn.openmap.gui.WindowSupport;

/**
 * A button that handles the time slider configuration.
 *
 */
public class ConfigureButton extends JButton implements ActionListener,
        LightMapHandlerChild, GUIConstants {
    
    private static final long serialVersionUID = -2966557439079679203L;
    
    protected WindowSupport ws;

    protected Frame frame = null;

    protected ConfigureSliderPanel csp;

    protected TimeSlider theSlider;

    public ConfigureButton(TimeSlider theSlider) {
        super("Configure");
        this.theSlider = theSlider;

        addActionListener(this);
        csp = new ConfigureSliderPanel(this);
    }

    public void actionPerformed(ActionEvent ae) {

        String command = ae.getActionCommand();

        if ((command.equals(OK)) && (ws != null)) {

            csp.updateValues();
            configureTimeSlider();
            ws.killWindow();
        } else if ((command.equals(CANCEL)) && (ws != null)) {

            ws.killWindow();
        } else {
            if (ws == null) {
                ws = new WindowSupport(csp, "Configure Slider");
            }

            ws.displayInWindow(frame);
        }
    }

    public void configureTimeSlider() {

        if (csp.getTimeResolution().equals(TimeSlider.YEAR)) {

            theSlider.setMinimum(csp.getYearMin());
            theSlider.setMaximum(csp.getYearMax());
            theSlider.setValue(csp.getYearMin());

            theSlider.setMajorTickSpacing(10);
            theSlider.setLabelTable(theSlider.createStandardLabels(10));
        } else if (csp.getTimeResolution().equals(TimeSlider.MONTH)) {

            theSlider.setMinimum(csp.getMonthMin());
            theSlider.setMaximum(csp.getMonthMax());
            theSlider.setValue(csp.getMonthMin());

            theSlider.setMajorTickSpacing(1);
            theSlider.setLabelTable(theSlider.createStandardLabels(1));
        } else {

            theSlider.setMinimum(csp.getDayMin());
            theSlider.setMaximum(csp.getDayMax());
            theSlider.setValue(csp.getDayMin());

            theSlider.setMajorTickSpacing(7);
            theSlider.setLabelTable(theSlider.createStandardLabels(7));
        }
        theSlider.setMinorTickSpacing(1);
        theSlider.setPaintTicks(true);
        theSlider.setPaintLabels(true);
        theSlider.setPaintTrack(true);
        theSlider.setSnapToTicks(true);

        theSlider.setTimeResolution(csp.getTimeResolution());
        theSlider.setMagMin(csp.getMagMin());
        theSlider.setMagMax(csp.getMagMax());
        theSlider.setMonthMin(csp.getMonthMin());
        theSlider.setYearMin(csp.getYearMin());
        theSlider.setRunQuery(true);

        theSlider.sendQueryParameters();

    }

    public void findAndInit(Object someObj) {

        if (someObj instanceof OpenMapFrame) {
            frame = (Frame) someObj;
        }

    }

    public void findAndUndo(Object someObj) {
        if (someObj instanceof OpenMapFrame) {
            frame = null;
        }
    }

}
